#!/usr/bin/env python3
# -*- encoding=utf-8 -*-

# install requires
# pip3 install elasticsearch==7.13.0 --target=/usr/local/lib/python3.7/site-packages
# pip3 install PyYAML --target=/usr/local/lib/python3.7/site-packages

# move data from ES/reproducible-test to ES/latest-reproducible-test
# as default, move latest 24 hrs data.
#
# and we can define move scope like:
# Usage:
#   update-latest group_id=reproducible-test-2022-05-25
#  


import sys
import time
import datetime
import os
import json
import argparse
from elasticsearch import Elasticsearch

#CURRENT_FILE_DIR = os.path.dirname(os.path.abspath(__file__))

#sys.path.append(f"{CURRENT_FILE_DIR}/")
#from reproducible_test import *

# ----------------- constants ----------------------------
INDEX_FROM = 'reproducible-test'
INDEX_TO = 'latest-reproducible-test'

ES_HOST = '123.60.67.76'
ES_PORT = '80'
ES_USER = 'root'
ES_PASSWORD = 'Helloman@12#$'

ES_CLIENT = Elasticsearch(
      [f"http://{ES_HOST}:{ES_PORT}/"],
      http_auth=(ES_USER, ES_PASSWORD)
      )

def parse_argv():
    result = {}
    params = sys.argv[1:]
    if len(params) > 0:
        for item in params:
            k, v = item.split('=')
            result.setdefault(k, v)
            result[k] = v

    return result

def query_body():
    query = {
        "query": {}
    }
    filter_params = parse_argv()

    if filter_params:
        query['query'] = {'term': filter_params}
	    
    else:
        now = datetime.datetime.now()
        time = datetime.datetime.strptime(str(now).split('.')[0], "%Y-%m-%d %H:%M:%S")
        end = now.strftime("%Y-%m-%dT%H:%M:%S+0800")
        start = time - datetime.timedelta(days = 1, hours = 1)
        start = start.strftime("%Y-%m-%dT%H:%M:%S+0800")

        query['query'] = {
	    'range': {
                "task_start_time": { # {'gte': '2022-05-24T16:17:49+0800', 'lte': '2022-05-25T17:17:49+0800'}
                    "gte": start,
                    "lte": end
                }
	    }
	}
    return query

def move_rpm_data(query, page_size, num):
    query['size'] = page_size
    query['from'] = num

    data = ES_CLIENT.search(
        index = INDEX_FROM,
        body = query
    )['hits']['hits']

    for doc in data:
        doc = doc['_source']
        if not doc.get('rpm_name'):
            continue

        print(doc.get('rpm_name'))
        ES_CLIENT.index(
            index = 'latest-reproducible-test',
            doc_type = '_doc',
            id = doc.get('rpm_name'),
            body = json.dumps(doc, indent=4)
            )

def update_latest_rpm():
    query = query_body()
    doc_count = ES_CLIENT.count(
        index = INDEX_FROM,
        body = query
    )
    doc_count = doc_count.get('count')
    page_size = 2000
    num = 0
    while doc_count > 0:
        move_rpm_data(query, page_size, num)

        doc_count -= page_size
        num += page_size

def move_repo_data(query, page_size, num):
    query['size'] = page_size
    query['from'] = num

    data = ES_CLIENT.search(
        index = 'repo-reproducible-test',
        body = query
    )['hits']['hits']

    for doc in data:
        doc = doc['_source']
        if not doc.get('pkg_name'):
            continue

        print(doc.get('pkg_name'))
        ES_CLIENT.index(
            index = 'latest-repo-reproducible-test',
            doc_type = '_doc',
            id = doc.get('pkg_name'),
            body = json.dumps(doc, indent=4)
            )

def update_latest_repo():
    query = query_body()
    doc_count = ES_CLIENT.count(
        index = 'repo-reproducible-test',
        body = query
    )
    doc_count = doc_count.get('count')
    page_size = 2000
    num = 0
    while doc_count > 0:
        move_repo_data(query, page_size, num)

        doc_count -= page_size
        num += page_size




if __name__ == "__main__":
    update_latest_rpm()
    update_latest_repo()
